package com.vhans.aapi.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.vhans.core.annotation.OptLogger;
import com.vhans.core.web.controller.BaseController;
import com.vhans.core.web.model.Result;
import com.vhans.core.web.model.dto.AgreementDTO;
import com.vhans.core.web.page.TableDataInfo;
import com.vhans.db.model.dto.ConditionDTO;
import com.vhans.db.model.vo.agreement.AgreementAllVO;
import com.vhans.db.model.vo.agreement.AgreementInfoAllVO;
import com.vhans.db.model.vo.agreement.AgreementInfoVO;
import com.vhans.db.service.IBAgreementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

import static com.vhans.core.constant.OptTypeConstant.*;

/**
 * 约起控制器
 *
 * @author vhans
 */
@Api(tags = "约起模块")
@RestController
public class AgreementController extends BaseController {

    @Autowired
    private IBAgreementService agreementService;

    @ApiOperation(value = "查看约起列表")
    @SaCheckPermission("agree:agreement:list")
    @GetMapping("/admin/agreement/list")
    public TableDataInfo listAdminVO(ConditionDTO condition) {
        startPage();
        List<AgreementAllVO> list = agreementService.listAgreementVO(condition);
        return getDataTable(list);
    }

    /**
     * 添加约起
     *
     * @param agreement 约起信息
     * @return {@link Result <>}
     */
    @OptLogger(value = ADD)
    @ApiOperation(value = "添加约起")
    @SaCheckPermission("agree:agreement:add")
    @PostMapping("/admin/agreement/add")
    public Result<?> addAgreement(@Validated @RequestBody AgreementDTO agreement) {
        agreementService.addAgreement(agreement, agreement.getAgreementType());
        return Result.success();
    }

    /**
     * 删除约起
     *
     * @param agreement 条件
     * @return {@link Result<>}
     */
    @OptLogger(value = DELETE)
    @ApiOperation(value = "删除约起")
    @SaCheckPermission("agree:agreement:delete")
    @DeleteMapping("/admin/agreement/delete")
    public Result<?> deleteAgreement(@RequestBody AgreementDTO agreement) {
        agreementService.deleteAgreement(agreement);
        return Result.success();
    }

    /**
     * 修改约起
     *
     * @param agreement 约起信息
     * @return {@link Result<>}
     */
    @OptLogger(value = UPDATE)
    @ApiOperation(value = "修改约起")
    @SaCheckPermission("agree:agreement:update")
    @PutMapping("/admin/agreement/update")
    public Result<?> updateAgreement(@RequestBody AgreementDTO agreement) {
        return Result.success(agreementService.updateAgreement(agreement, agreement.getAgreementType()));
    }

    /**
     * 编辑约起
     *
     * @param agreement   条件
     * @return {@link Result<AgreementInfoAllVO>} 约起info
     */
    @ApiOperation(value = "编辑约起")
    @SaCheckPermission("agree:agreement:edit")
    @PostMapping("/admin/agreement/edit")
    public Result<AgreementInfoAllVO> editAgreement(@RequestBody AgreementDTO agreement) {
        return Result.success(agreementService.editAgreement(agreement));
    }

    /**
     * 上传约起图片
     *
     * @param file 文件
     * @return {@link Result<String>} 约起图片地址
     */
    @OptLogger(value = UPLOAD)
    @ApiOperation(value = "上传约起图片")
    @ApiImplicitParam(name = "file", value = "约起图片", required = true, dataType = "MultipartFile")
    @SaCheckPermission("agree:agreement:edit")
    @PostMapping("/admin/agreementImage/upload")
    public Result<String> saveQuizImages(@RequestParam("file") MultipartFile file) {
        return Result.success(agreementService.saveAgreementImages(file));
    }
}
